package com.sitanyun.merchant.guide.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.Loading_view;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * web界面
 */
public class AgreementActivity extends BaseActivity {


    @BindView(R.id.tv_return)
    ImageView tvReturn;
    @BindView(R.id.tv_title_str)
    TextView tvTitleStr;
    @BindView(R.id.image_right_key)
    ImageView imageRightKey;
    @BindView(R.id.tv_right_key)
    TextView tvRightKey;
    @BindView(R.id.ff_main_layout)
    FrameLayout ffMainLayout;
    @BindView(R.id.webview)
    WebView webview;
    private String weburl;

    @Override
    public int getLayoutId() {
        return R.layout.activity_agreement;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        if (getIntent().getStringExtra("weburl").equals("1")){
            setTitleStr("用户协议");
        }else if (getIntent().getStringExtra("weburl").equals("2")){
            setTitleStr("用户隐私协议");
        }else if (getIntent().getStringExtra("weburl").equals("3")){
            setTitleStr("电子签约服务协议");
        } else if (getIntent().getStringExtra("weburl").equals("4")){
            setTitleStr("疑难解答");
        }
    }

    @SuppressLint("JavascriptInterface")
    @Override
    public void initView() {
      Loading_view  loading_view = new Loading_view(AgreementActivity.this, R.style.CustomDialog);
        weburl = getIntent().getStringExtra("weburl");
        webview.addJavascriptInterface(AgreementActivity.this, "android");//添加js监听 这样html就能调用客户端
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        webview.setWebChromeClient(new WebChromeClient());
        webview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((WebView) v).requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
              //  progressDialog.show();
                loading_view.show();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
              //  progressDialog.dismiss();
                loading_view.dismiss();
            }

            // 重写此方法可以让webview处理https请求
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
                handler.proceed();
            }
        });
        //  1 用户协议  2 用户隐私协议
        if (!weburl.isEmpty()){
            if (weburl.equals("1")){
                webview.loadUrl(Urls.serviceAgreement);
            }else if (weburl.equals("2")){
                webview.loadUrl(Urls.privacyAgreement);
            }else if (weburl.equals("3")){
                webview.loadUrl(Urls.cooperationAgreement);
            }else {
                webview.loadUrl(Urls.troubleshoot);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
